package com.yzkj.cpjxc.business.service.impl;

import com.yzkj.cpjxc.business.constants.SysDeptConstants;
import com.yzkj.cpjxc.business.domain.SysDept;
import com.yzkj.cpjxc.business.domain.SysDeptShowVo;
import com.yzkj.cpjxc.business.domain.ZTreeComm;
import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.framework.utils.IDUtils;
import com.yzkj.cpjxc.business.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门管理 Service 实现
 */
@Service
public class SysDeptServiceImpl implements SysDeptService {

	/**
	 * MyBatis 命名空间
	 */
	private final static String MAPPER_NAMESPACE = "com.chkj.sysMgr.securityMgr.sysDept.mappers.SysDeptMapper";

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Transactional
	@Override
	public BusinessMap create(SysDept sysDept) throws Exception {
		
		String deptId = sysDept.getDeptId();
		sysDept.setDeptId(null);
		//同一父层级下不能有相同的部门名称
		BusinessMap bm = new BusinessMap();
		if (this.isExists(sysDept)) {
			bm.put(Constants.WARNING_MSG, "该部门名称(同一上级部门下不能存在相同的部门中文名称或英文名称)已经存在");
			return bm;
		}
		sysDept.setDeptId(deptId);
		sqlSessionTemplate.insert(MAPPER_NAMESPACE + ".add", sysDept);
		return bm;
	}

	@Override
	public boolean isExists(SysDept sysDept) throws Exception {
		return (Integer)sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".isExists", sysDept) > 0;
	}
	
	@Override
	public SysDeptShowVo getById(String sysDeptId) throws Exception {
		if(StringUtils.isNotBlank(sysDeptId)){
			return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getById", sysDeptId);
		}
		return null;
	}
	
	@Transactional
	@Override
	public BusinessMap edit(SysDept sysDept) throws Exception {
		BusinessMap bm = new BusinessMap();
		if (this.isExists(sysDept)) {
			bm.put(Constants.WARNING_MSG, "该部门名称(同一上级部门下不能存在相同的部门中文名称或英文名称)已经存在");
			return bm;
		}
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".edit", sysDept);
		return bm;
	}

	@Override
	public Pager<SysDeptShowVo> search(Pager<SysDeptShowVo> pager, SysDept sysDept) throws Exception {
		List dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".page", sysDept, new RowBounds((pager.getCurrentPage() - 1) * pager.getLimit(), pager.getLimit()));
		pager.setDataList(dataList);
		
		// 如果需要转换成ShowVo展示，那么就在这里转换
		
		Long total = sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTotal", sysDept);
		pager.setTotal(total);
		return pager;
	}

	@Override
	public List<ZTreeComm> queryRootDeptTreeList(String id) throws Exception {
		List<ZTreeComm> dataList = new ArrayList<ZTreeComm>();
		if (StringUtils.isEmpty(id)) {
			//id为空,初始加载，查询根节点
			dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".queryRootDeptTreeList", id);
		} else {
			//id不为空，为下拉，查询子节点
			dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".queryDeptTreeList", id);
		}
		return dataList;
	}

	@Override
	public boolean isParent(String id) throws Exception {
		int count = (int) sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".isParent", id);
		return count > 0;
	}

	@Override
	public SysDept getTopLevelDept() throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTopLevelDept");
	}

	@Override
	public boolean delete(String deptId) throws Exception {
		if(StringUtils.isNotBlank(deptId)){
			//删除当前部门
			sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".delete", deptId);
			return true;
		}
		return false;
	}

	@Override
	public List<SysDeptShowVo> getChlidDeptList(String parentDeptId) throws Exception {
		if(StringUtils.isBlank(parentDeptId)){
			return null;
		}
		List<SysDeptShowVo> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".getChlidDeptList", parentDeptId);
		return dataList;
	}

	@Override
	public List<SysDeptShowVo> getDeptByUserId(String userId) throws Exception {
		if(StringUtils.isBlank(userId)){
			return null;
		}
		List<SysDeptShowVo> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".getDeptByUserId", userId);
		return dataList;
	}

	@Override
	public void initData() throws Exception{
		//判断没有部门数据，则添加系统初始化顶级部门
		Long total = sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTotal", new SysDept());
		if(total<1){
			SysDept sd = new SysDept();
			sd.setDeptId(IDUtils.genUUID());
			sd.setDeptCnName(Constants.DJ_DEPT_CN_NAME);
			sd.setDeptEnName(Constants.DJ_DEPT_EN_NAME);
			sd.setDeptReferred(Constants.DJ_DEPT_REFERREDE);
			sd.setIsFiction(SysDeptConstants.FICTION_NO);
			sd.setIsStatistics(SysDeptConstants.STATISTICS_NO);
			sd.setRemark("这是顶级部门");
			sd.setDeleteFlag(Constants.DELETE_FLAG_NO);
			this.create(sd);
		}
	}

}
